package nowcoder.written_examination.pdd;

import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 *
 * @author fance
 * @date 18-6-26 上午6:11
 */
class Bear implements Comparable<Integer>{
    @Override
    public int compareTo(Integer o) {
        return this.x - o;
    }

    public int x;
    public int y;

    public Bear(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class MainI {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            int[] candy = new int[m];
            for (int i = 0; i < m; i++) {
                candy[i] = scanner.nextInt();
            }
            Bear[] bear = new Bear[n];
            for (int i = 0; i < n; i++) {
                bear[i] = new Bear(scanner.nextInt(),scanner.nextInt());
            }
            PriorityQueue<Bear> priorityQueue = new PriorityQueue<>();
            for (int i = 0; i < n; i++) {
                priorityQueue.offer(bear[i]);
            }
            Arrays.sort(candy);
            while (!priorityQueue.isEmpty()) {
                Bear cur = priorityQueue.poll();
                int hungry = cur.y;
                for (int j = m - 1; j >= 0; j--) {
                    if (candy[j] == -1) {
                        continue;
                    }
                    if (candy[j] <= hungry) {
                        hungry -= candy[j];
                        candy[j] = -1;
                    }
                    if (hungry == 0) {
                        break;
                    }
                }
                cur.y = hungry;
            }

            for (int i = 0; i < n; i++) {
                System.out.println(bear[i].y);
            }
        }
    }
}
    /*public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            Point[] points = new Point[n];
            for (int i = 0; i < n; i++) {
                points[i] = new Point(scanner.nextInt(),scanner.nextInt());
            }
            int cnt = 0;
            for (int i = 0; i < n - 2; i++) {
                for (int j = i + 1; j < n - 1; j++) {
                    for (int k = j + 1; k < n; k++) {
                        double a = Math.sqrt((points[i].x - points[j].x) * (points[i].x - points[j].x) +
                                (points[i].y - points[j].y) * (points[i].y - points[j].y));
                        double b = Math.sqrt((points[i].x - points[k].x) * (points[i].x - points[k].x) +
                                (points[i].y - points[k].y) * (points[i].y - points[k].y));
                        double c = Math.sqrt((points[k].x - points[j].x) * (points[k].x - points[j].x) +
                                (points[k].y - points[j].y) * (points[k].y - points[j].y));

                        if (a + b - c > 0.00000001 && b + c - a > 0.00000001 && a + c - b > 0.00000001) {
                            cnt++;
                        }
                    }
                }
            }
            System.out.println(cnt);
        }
    }
}*/
    /*public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextInt();
            }
            int max1 = Integer.MIN_VALUE;
            int max2 = Integer.MIN_VALUE;
            int max3 = Integer.MIN_VALUE;
            int min1 = Integer.MAX_VALUE;
            int min2 = Integer.MAX_VALUE;
            int res = 0;
            for (int i = 0; i < n; i++) {
                int cur = a[i];
                if (cur != 0) {
                    if (cur > max1) {
                        max3 = max2;
                        max2 = max1;
                        max1 = cur;
                    } else if (cur > max2) {
                        max3 = max2;
                        max2 = cur;
                    } else if (cur > max3) {
                        max3 = cur;
                    }

                    if (cur < min1) {
                        min2 = min1;
                        min1 = cur;
                    } else if (cur < min2) {
                        min2 = cur;
                    }
                    res = Math.max(max1 * max2 * max3,min1 * min2* max1);
                }

            }
            System.out.println(res);
        }
    }
}*/
    /*public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        List<Integer> list = new ArrayList<>();
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextInt();
            }
            int max1 = Integer.MIN_VALUE;
            int max2 = Integer.MIN_VALUE;
            int max3 = Integer.MIN_VALUE;
            int min1 = Integer.MAX_VALUE;
            int min2 = Integer.MAX_VALUE;
            int res = 0;
            for (int i = 0; i < n; i++) {
                int cur = a[i];
                if (cur != 0) {
                    if (cur > max1) {
                        max3 = max2;
                        max2 = max1;
                        max1 = cur;
                    } else if (cur > max2) {
                        max3 = max2;
                        max2 = cur;
                    } else if (cur > max3) {
                        max3 = cur;
                    } else if (cur < min1) {
                        min2 = min1;
                        min1 = cur;
                    } else if (cur < min2) {
                        min2 = cur;
                    }
                } else {
                    continue;
                }
                res = Math.max(max1 * max2 * max3,max1 * min1 * min2);
            }

            System.out.println(res);
        }
    }
}*/
            /*int[] x = new int[n];
            int[] y = new int[n];
            for (int i = 0; i < n; i++) {
                x[i] = scanner.nextInt();
                y[i] = scanner.nextInt();
            }
            int cnt = 0;
            for (int i = 0; i < n - 2; i++) {
                for (int j = i + 1; j < n - 1; j++) {
                    for (int k = j + 1; k < n; k++) {
                        double a =  Math.sqrt(Math.pow(Math.abs(x[i] - x[j]),2) + Math.pow(Math.abs(y[i] - y[j]),2));
                        double b = Math.sqrt(Math.pow(Math.abs(x[i] - x[k]),2) + Math.pow(Math.abs(y[i] - y[k]),2));
                        double c =  Math.sqrt(Math.pow(Math.abs(x[k] - x[j]),2) + Math.pow(Math.abs(y[k] - y[j]),2));
                        if (Double.compare(a+b,c) > 0&&Double.compare(a+c,b) > 0&&Double.compare(c+b,a) > 0) {

                            cnt++;
                        }
                    }
                }
            }
            System.out.println(cnt);
        }
    }
}*/
           /* int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = scanner.nextInt();
            }
            int m = scanner.nextInt();
            int[] w = new int[m];
            for (int i = 0; i < m; i++) {
                w[i] = scanner.nextInt();
            }

            System.out.println(cnt(w,h));
        }
    }
    public static int cnt(int[] w,int[] h) {
        int cnt = 0;
        Arrays.sort(w);
        Arrays.sort(h);
        int i = 0;
        int j = 0;
        while (i < w.length && j < h.length) {
            if (w[i] >= h[j]) {
                cnt++;
                j++;
            }
            i++;
        }
        return cnt;
    }
}*/
       /* int max = Integer.MIN_VALUE;
        for (int i = 0; i < list.size() - 2; i++) {
            for (int j = i + 1; j < list.size() - 1; j++) {
                for (int k = j + 1; k < list.size(); k++) {
                    max = Math.max(max,list.get(i) * list.get(j) * list.get(k));
                }
            }
        }
        System.out.println(max);
    }
}*/
